package com.blw.democar.config;

/**
 * @author shetiefeng
 * @date 2017-11-10 19:06
 **/

import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.UUID;

/**  自定义填充  */
public class MybatisPlusMetaObjectHandler extends MetaObjectHandler {

    /**
     * 新增时填入数据
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        TableInfo table = TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass());
        if (table.getIdType() == IdType.INPUT) {
            if(StringUtils.isEmpty((String)metaObject.getValue(table.getKeyProperty()))){
                metaObject.setValue(table.getKeyProperty(), UUID.randomUUID().toString());
            }
        }
    }

    /**
     * 修改时填入数据
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
    }
}
